﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Xnaml.Demo {
	public class Gohan {
		private Texture2D texture = null;
		private Rectangle source = Rectangle.Empty;
		private string state = "Default State";
		private bool repeat = false, reverseanimation = false;
		private Dictionary<string, List<Rectangle>> anims = new Dictionary<string, List<Rectangle>>( );
		private IntInterpolator frame = new IntInterpolator( 0 );
		public Vector2 Position = Vector2.Zero, Scale = Vector2.One, Origin = Vector2.Zero;
		public float Rotation = 0.0f, Layer = 0.0f;
		public IntInterpolator Alpha = new IntInterpolator( 0 );
		public Color Tint = Color.White;
		public SpriteEffects spriteeffects = SpriteEffects.None;

		public string State {
			get {
				return state;
			}
		}

		public Vector2 Size {
			get {
				return new Vector2( source.Width, source.Height );
			}
		}

		public Gohan ( ) {
			repeat = true;
			anims.Add( "", new List<Rectangle>( ) );
			anims.Add( "Appear", new List<Rectangle>( ) );
			anims.Add( "Stand", new List<Rectangle>( ) );
			anims.Add( "Walk", new List<Rectangle>( ) );
			anims.Add( "Jump", new List<Rectangle>( ) );
			anims.Add( "Power Up", new List<Rectangle>( ) );
			int i;
			Rectangle src;

			src = new Rectangle( 0, 0, 72, 62 );
			for ( i = 0; i < 10; ++i ) {
				anims[ "Appear" ].Add( src );
				src.Offset( 75, 0 );
			}

			src = new Rectangle( 0, 78, 61, 64 );
			for ( i = 0; i < 3; ++i ) {
				anims[ "Stand" ].Add( src );
				anims[ "" ].Add( src );
				src.Offset( 61, 0 );
			}

			src = new Rectangle( 192, 80, 53, 64 );
			for ( i = 0; i < 3; ++i ) {
				anims[ "Walk" ].Add( src );
				src.Offset( 55, 0 );
			}

			src = new Rectangle( 434, 69, 55, 89 );
			for ( i = 0; i < 3; ++i ) {
				anims[ "Jump" ].Add( src );
				src.Offset( 55, 0 );
			}


			anims[ "Power Up" ].Add( new Rectangle( 67, 230, 67, 90 ) );
			anims[ "Power Up" ].Add( new Rectangle( 133, 230, 67, 90 ) );
			anims[ "Power Up" ].Add( new Rectangle( 203, 230, 67, 90 ) );

			Animate( "", 250, true, true );
			Alpha.Reset( );
		}

		public void Load ( ContentManager content ) {
			texture = content.Load<Texture2D>( "Gohan" );
		}

		public void Update ( GameTime gametime ) {
			frame.Update( gametime );
			Alpha.Update( gametime );
			if ( frame.Finished && !frame.Active ) {
				if ( repeat ) {
					if ( reverseanimation )
						frame.RestartReverse( );
					else
						frame.Restart( );
				}
				else {
					if ( state == "Appear" ) {
						Animate( "Stand", 250, true, true );
					}
				}
			}
			source = anims[ state ][ frame.Value ];
		}

		public void Animate ( string State, double MSPerFrame, bool Repeat, bool Reverse ) {
			if ( anims.ContainsKey( State ) ) {
				if ( state != State ) {
					frame.Reset( );
					if ( anims[ State ].Count > 0 )
						frame.Start( 0, anims[ State ].Count - 1, MSPerFrame * anims[ State ].Count );
					else
						frame.Start( 0, 0, 0 );
				}
				reverseanimation = Reverse;
				repeat = Repeat;
				state = State;
			}
			else {
				if (state != "") {
					frame.Reset( );
					if ( anims[ "" ].Count > 0 )
						frame.Start( 0, anims[ "" ].Count - 1, MSPerFrame * anims[ "" ].Count );
					else
						frame.Start( 0, 0, 0 );
				}
				reverseanimation = true;
				state = "";
				repeat = true;
			}
		}

		public void Render ( SpriteBatch spritebatch ) {
			if ( texture != null ) {
				if ( source.IsEmpty )
					spritebatch.Draw( texture, Position, null, Tint * Alpha.Progress, Rotation, Origin, Scale, spriteeffects, Layer );
				else
					spritebatch.Draw( texture, Position + ( state == "Power Up" ? new Vector2( 0, 15 ) : Vector2.Zero ), source, Tint * Alpha.Progress, Rotation, Origin, Scale, spriteeffects, Layer );
			}
		}
	}
}
